package org.deri.socializer;


import org.deri.socializer.activities.chat.ChatActivity;
import org.deri.socializer.com.http.Surfer;
import org.deri.socializer.com.http.Walker;
import org.deri.socializer.com.http.Website;
import org.deri.socializer.com.xmpp.XmppClient;
import org.deri.socializer.com.xmpp.XmppGroupChat;
import org.deri.socializer.util.XMPPUtilities;
import org.deri.vor.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.maps.MapView;

public class NotificationPanel {
	
	public static final byte BUTTON_ONE = 1;
	public static final byte BUTTON_TWO = 2;
	public static final byte BUTTON_THREE = 3;
	
	
	SocializerApplication app;
	Activity activity;
    View popup;
    MapView map;
    Button goToChatButton;
    String domain = "";
    boolean isVisible = false;
    int viewID;
    
    public NotificationPanel(SocializerApplication app, Activity activity, int layout, int viewID) {
    	this.app = app;
    	this.activity = activity;
    	this.viewID = viewID;

    	ViewGroup parent = (ViewGroup) this.activity.findViewById(viewID);
    	this.popup = activity.getLayoutInflater().inflate(layout, parent, false);
        this.popup.setPadding(10, 10, 10, 10);
        
    	popup.setOnClickListener(new View.OnClickListener() {
    		public void onClick(View v) {
    			hide();
    		}
    	});
    	
    }
    
    public View getView() {
      return(popup);
    }
    
    public void setDomain(String domain) {
    	this.domain = domain;
    }
    
    public void show(boolean alignTop) {
    	hide();

    	RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
    		RelativeLayout.LayoutParams.WRAP_CONTENT,
    		RelativeLayout.LayoutParams.WRAP_CONTENT
    	);
    	System.out.println("[show] 2");
    	if (alignTop) {
    		lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    		//lp.setMargins(0, 20, 0, 0);
    	} else {
    		lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    		lp.setMargins(0, 0, 0, 60);
    	}
    	ViewGroup group = (ViewGroup) this.activity.findViewById(this.viewID);
    	System.out.println("[PopupPanel.show] before addView");
    	try {
    		group.addView(popup, lp);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	System.out.println("[PopupPanel.show] after addView");
    	group.invalidate();
    	isVisible=true;
    }
    
    public void hide() {
    	if (isVisible) {
    		isVisible=false;
    		((ViewGroup)this.popup.getParent()).removeView(this.popup);
    	}
    }
    
    
    public void setTextViewText(String text) {
    	TextView textView = (TextView)this.getView().findViewById(R.id.popupTextView);
    	textView.setText(text);
    }
    
    public void setButtonText(byte buttonNumber, String text) {
    	Button button = null;
    	switch (buttonNumber) {
    	case BUTTON_ONE: 
    		button = (Button) this.getView().findViewById(R.id.popupButton1);
    		break;
       	case BUTTON_TWO: 
       		button = (Button) this.getView().findViewById(R.id.popupButton2);
       		break;
       	case BUTTON_THREE: 
       		button = (Button) this.getView().findViewById(R.id.popupButton3);
       		break;
       	default:
       		button = null;
       		break;
    	}
    	if (button != null)
    		button.setText(text);
    }
    
    public void setButtonVisibility(byte buttonNumber, int visibility) {
    	Button button = null;
    	switch (buttonNumber) {
    	case BUTTON_ONE: 
    		button = (Button) this.getView().findViewById(R.id.popupButton1);
    		break;
       	case BUTTON_TWO: 
       		button = (Button) this.getView().findViewById(R.id.popupButton2);
       		break;
       	case BUTTON_THREE: 
       		button = (Button) this.getView().findViewById(R.id.popupButton3);
       		break;
       	default:
       		button = null;
       		break;
    	}
    	if (button != null)
    		button.setVisibility(visibility);
    }
 
    
    public void setButtonOnClickListener(byte buttonNumber, OnClickListener listener) {
    	Button button = null;
    	switch (buttonNumber) {
    	case BUTTON_ONE: 
    		button = (Button) this.getView().findViewById(R.id.popupButton1);
    		break;
       	case BUTTON_TWO: 
       		button = (Button) this.getView().findViewById(R.id.popupButton2);
       		break;
       	case BUTTON_THREE: 
       		button = (Button) this.getView().findViewById(R.id.popupButton3);
       		break;
       	default:
       		button = null;
       		break;
    	}
    	if (button != null)
    		button.setOnClickListener(listener);
    }
    
    
    
    public void showNewPrivateChatMessageNotification(Activity activity, XmppClient xmppClient, boolean alignTop) {
    	final Activity finalActivity = activity;
    	final String userJid = this.app.lastMessage.getFrom();
    	this.setTextViewText(XMPPUtilities.getSenderName(this.app.lastMessage.getFrom()) + " wrote: \"" + this.app.lastMessage.getBody()  + "\"");
    	this.setButtonText(NotificationPanel.BUTTON_ONE, "Reply Now");
    	this.setButtonText(NotificationPanel.BUTTON_TWO, "Reply Later");
    	this.setButtonText(NotificationPanel.BUTTON_THREE, "Ignore");
    	
    	this.setButtonOnClickListener(NotificationPanel.BUTTON_ONE, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	goToPrivateChat(finalActivity, userJid);
            }
        });
    	
    	this.setButtonOnClickListener(NotificationPanel.BUTTON_THREE, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	closePrivateChat(userJid);
            }
        });
    	
    	this.setButtonOnClickListener(NotificationPanel.BUTTON_TWO, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            }
        });
	    
	    this.show(alignTop);
    }
    
    public void showNewGroupChatMessageNotification(Activity activity, XmppClient xmppClient, boolean alignTop) {
    	final Activity finalActivity = activity;
       	final String user = XMPPUtilities.getUser(this.app.lastMessage.getFrom());
    	final String room = XMPPUtilities.getRoomName(this.app.lastMessage.getFrom());
    	this.setTextViewText("In " + room + ", " + user +" wrote:\n\""+ this.app.lastMessage.getBody()  + "\"");
    	this.setButtonText(NotificationPanel.BUTTON_ONE, "Join Chat");
    	this.setButtonText(NotificationPanel.BUTTON_TWO, "Check Later");
    	this.setButtonText(NotificationPanel.BUTTON_THREE, "Leave Chat");

    	this.setButtonOnClickListener(NotificationPanel.BUTTON_ONE, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	goToGroupChat(finalActivity, room);
            }
        });

    	this.setButtonOnClickListener(NotificationPanel.BUTTON_THREE, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	leaveGroupChat(finalActivity, room);
            }
        });
    	
    	this.setButtonOnClickListener(NotificationPanel.BUTTON_TWO, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            }
        });
	    
	    this.show(alignTop);
    }

    
    public void showJoinVirtualLocationNotification(Activity activity, XmppClient xmppClient, Website location, boolean alignTop) {
    	final Activity finalActivity = activity;
    	final String room = location.getVLoc();
    	final String url = location.getUrl();
    	this.setTextViewText(location.getVLoc());
    	this.setButtonText(NotificationPanel.BUTTON_ONE, "Join");
    	this.setButtonText(NotificationPanel.BUTTON_TWO, "Close");
    	this.setButtonText(NotificationPanel.BUTTON_THREE, "Open Browser");

    	this.setButtonOnClickListener(NotificationPanel.BUTTON_ONE, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	goToWebGroupChat(finalActivity, room);
            }
        });

    	this.setButtonOnClickListener(NotificationPanel.BUTTON_TWO, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            }
        });
	    
    	this.setButtonOnClickListener(NotificationPanel.BUTTON_THREE, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	goToWebsite(url);
            }
        });
    	
	    this.show(alignTop);
    }

    
    public void showSurferActionNotification(Activity activity, XmppClient xmppClient, Surfer surfer, boolean alignTop) {
    	final Activity finalActivity = activity;
    	final String room = surfer.getDomain();
    	final String userJid = surfer.getJid();
    	this.setTextViewText(surfer.getName() + " is at location " + surfer.getDomain());
    	this.setButtonText(NotificationPanel.BUTTON_ONE, "Group Chat");
    	this.setButtonText(NotificationPanel.BUTTON_TWO, "Private Chat");

    	this.setButtonOnClickListener(NotificationPanel.BUTTON_ONE, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	goToWebGroupChat(finalActivity, room);
            }
        });

    	this.setButtonOnClickListener(NotificationPanel.BUTTON_TWO, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	goToPrivateChat(finalActivity, userJid);
            }
        });
	    
    	this.setButtonVisibility(NotificationPanel.BUTTON_THREE, View.INVISIBLE);
    	
	    this.show(alignTop);
    }


    public void showWalkerActionNotification(Activity activity, XmppClient xmppClient, Walker walker, boolean alignTop) {
    	final Activity finalActivity = activity;
    	final String userJid = walker.getJid();
    	this.setTextViewText(walker.getName() + " is at location\n[" + walker.getLongitude() + "," + walker.getLatitude() + "]");
    	this.setButtonText(NotificationPanel.BUTTON_ONE, "Private Chat");
    	this.setButtonText(NotificationPanel.BUTTON_TWO, "Close");

    	this.setButtonOnClickListener(NotificationPanel.BUTTON_ONE, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            	goToPrivateChat(finalActivity, userJid);
            }
        });

    	this.setButtonOnClickListener(NotificationPanel.BUTTON_TWO, new View.OnClickListener() {
            public void onClick(View v) {
            	hide();
            }
        });
	    
    	this.setButtonVisibility(NotificationPanel.BUTTON_THREE, View.INVISIBLE);
    	
	    this.show(alignTop);
    }
    
    private void goToWebGroupChat(Activity activity, String room) {
    	this.goToGroupChat(activity, room + SocializerApplication.XMPP_GROUP_CHAT_SUFFIX_WEB);
    }
    
    private void goToGroupChat(Activity activity, String room) {
    	System.out.println("[NotificationPanel.goToGroupChat] room = " + room);
    	XmppGroupChat groupChat = this.app.getXMPPClient().createGroupChat(room);
    	groupChat.join();
    	this.app.getXMPPClient().setLastXmppWebChat(groupChat);
    	Intent intent = new Intent(activity.getApplicationContext(), ChatActivity.class);
    	activity.startActivity(intent);
    }
    
    private void goToPrivateChat(Activity activity, String userJid) {
    	this.app.getXMPPClient().setLastXmppWebChat(this.app.getXMPPClient().getXmppPrivateChat(userJid, true));
    	Intent intent = new Intent(activity.getApplicationContext(), ChatActivity.class);
    	activity.startActivity(intent);
    }
	
    private void leaveGroupChat(Activity activity, String room) {
    	this.app.getXMPPClient().leaveGroupChat(room);
    	this.app.getXMPPClient().setLastXmppWebChat(null);
    }
	
    private void closePrivateChat(String userJid) {
    	this.app.getXMPPClient().closePrivateChat(userJid);
    	this.app.getXMPPClient().setLastXmppWebChat(null);
    }
    
    private void goToWebsite(String url) {
    	if (!url.startsWith("http://") && !url.startsWith("https://"))
    		   url = "http://" + url;

    	Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    	browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    	this.app.startActivity(browserIntent);

    }
}